package crontask

import (
    "time"
    "reflect"
    "github.com/astaxie/beego/logs"
    "fmt"
    "cloud_worm/util/third/unotify"
    "cloud_worm/util/umodels/mysqldb/dbtask"
)

// 每执行一次cron task，写入一条记录
func createMarkForCronJob(cronId int64, label string) (int64, error) {
    //do := dbtask.WolCronTaskDoCount{
    //    CronId: cronId,
    //    Label: label,
    //    Created: time.Now().Unix(),
    //}
    //do.Updated = do.Created
    //err := do.Insert()
    //if err != nil {
    //    return 0, err
    //}
    //return do.Id, nil
    return 0, nil
}

/**
 @Description：crontask执行完成时，更新update，标记完成时间
 @Param:
 @Return：
 */
func updateMarkForCronJob(id int64) error {
    //do := dbtask.WolCronTaskDoCount{
    //    Id: id,
    //    Updated: time.Now().Unix(),
    //}
    //err := do.Update("updated")
    //if err != nil {
    //    return err
    //}
    //return nil
    return nil
}


/**
 @Description：crontask job begin
 @Param:
 @Return：
 */
func CronJobBegin(j interface{}) (int64, error) {

    val := reflect.ValueOf(j)
    name := reflect.Indirect(val).Type().Name()
    task := &dbtask.CronTask{
        Target: name,
    }
    logs.Info(fmt.Sprintf("%s begin ......", name))

    item := &unotify.IMNotifyItem{
        Grade:       unotify.InfoGrade,
        Title:       "计划任务",
        Msg:         fmt.Sprintf("%s--%s Begin...", time.Now().Format("2006-01-02 15:04:05"), name),
        IMUsers: []string{
            "用学",
        },
    }
    unotify.IMNotifyInst.SendSync(item)

    var recordID int64
    err := task.Read("target")
    if err == nil {
        recordID, err = createMarkForCronJob(task.Id, name)
        if err != nil {
            logs.Error(fmt.Sprintf("insert cron task mark error: %s", err.Error()))
            return 0, err
        }
    } else {
        return 0, err
    }

    return recordID, nil
}

/**
 @Description：crontask job end
 @Param:
 @Return：
 */
func CronJobEnd(recordID int64, j interface{}, errMsg string) error {

    val := reflect.ValueOf(j)
    name := reflect.Indirect(val).Type().Name()
    logs.Info(fmt.Sprintf("%s end ......", name))

    item := &unotify.IMNotifyItem{
        Grade:       unotify.InfoGrade,
        Title:       "计划任务",
        Msg:         fmt.Sprintf("%s--%s End: %s", time.Now().Format("2006-01-02 15:04:05"), name, errMsg),
        IMUsers: []string{
            "用学",
        },
    }
    unotify.IMNotifyInst.SendSync(item)

    return updateMarkForCronJob(recordID)
}

/**
 @Description：cron weapon task begin
 @Param:
 @Return：
 */
func CronWeaponTaskBegin(j interface{}) (int64, error) {
    task := j.(CrWeaponTask)

    logs.Info(fmt.Sprintf("%s begin ......", task.Name))
    recordID, err := createMarkForCronJob(task.Id, task.Name)
    if err != nil {
        logs.Error(fmt.Sprintf("insert cron task mark error: %s", err.Error()))
        return 0, err
    }

    return recordID, nil
}

/**
 @Description：cron weapon task end
 @Param:
 @Return：
 */
func CronWeaponTaskEnd(recordID int64, j interface{}) error {
    task := j.(CrWeaponTask)

    logs.Info(fmt.Sprintf("%s end ......", task.Name))
    return updateMarkForCronJob(recordID)
}